//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jic.member.utils;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

@Slf4j
public class DigestUtil {
    private static final char[] DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public DigestUtil() {
    }

    public static byte[] MD5(String str) {
        try {
            if (str == null) {
                return null;
            } else {
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                return messageDigest.digest(str.getBytes("utf8"));
            }
        } catch (Exception var2) {
            log.error(var2.toString());
            return null;
        }
    }

    public static String MD5Str(String str) {
        return new String(bytes2hex(MD5(str)));
    }

    public static byte[] SHA(String str) {
        try {
            if (str == null) {
                return null;
            } else {
                MessageDigest md = MessageDigest.getInstance("SHA-1");
                return md.digest(str.getBytes("utf8"));
            }
        } catch (Exception var2) {
            log.error(var2.toString());
            return null;
        }
    }

    public static String genKeyDES() {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
            keyGenerator.init(56);
            SecretKey key = keyGenerator.generateKey();
            String base64Str = Base64.getEncoder().encodeToString(key.getEncoded());
            return base64Str;
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static SecretKey loadKeyDES(String base64Key) {
        try {
            byte[] bytes = Base64.getDecoder().decode(base64Key);
            SecretKey key = new SecretKeySpec(bytes, "DES");
            return key;
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static byte[] encryptDES(byte[] source, SecretKey key) {
        try {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(1, key);
            return cipher.doFinal(source);
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static byte[] decryptDES(byte[] source, SecretKey key) {
        try {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(2, key);
            byte[] bytes = cipher.doFinal(source);
            return bytes;
        } catch (Exception var4) {
           log.error(var4.toString());
            return null;
        }
    }

    public static String genKeyAES() {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);
            SecretKey key = keyGenerator.generateKey();
            String base64Str = Base64.getEncoder().encodeToString(key.getEncoded());
            return base64Str;
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static SecretKey loadKeyAES(String base64Key) {
        try {
            byte[] bytes = Base64.getDecoder().decode(base64Key);
            SecretKey key = new SecretKeySpec(bytes, "AES");
            return key;
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static byte[] encryptAES(byte[] source, SecretKey key) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(1, key);
            return cipher.doFinal(source);
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static byte[] decryptAES(byte[] source, SecretKey key) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, key);
            return cipher.doFinal(source);
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static KeyPair getKeyPair() {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(512);
            return keyPairGenerator.generateKeyPair();
        } catch (Exception var1) {
            log.error(var1.toString());
            return null;
        }
    }

    public static String getPublicKey(KeyPair keyPair) {
        try {
            PublicKey publicKey = keyPair.getPublic();
            byte[] bytes = publicKey.getEncoded();
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static String getPrivateKey(KeyPair keyPair) {
        try {
            PrivateKey privateKey = keyPair.getPrivate();
            byte[] bytes = privateKey.getEncoded();
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static PublicKey loadPublicKey(String pubStr) {
        try {
            byte[] keyBytes = Base64.getDecoder().decode(pubStr);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(keySpec);
            return publicKey;
        } catch (Exception var5) {
           log.error(var5.toString());
            return null;
        }
    }

    public static PrivateKey loadPrivateKey(String priStr) {
        try {
            byte[] keyBytes = Base64.getDecoder().decode(priStr);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
            return privateKey;
        } catch (Exception var5) {
           log.error(var5.toString());
            return null;
        }
    }

    public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) {
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(1, publicKey);
            return cipher.doFinal(content);
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) {
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(2, privateKey);
            return cipher.doFinal(content);
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static byte[] sign(byte[] content, PrivateKey privateKey) {
        try {
            Signature signature = Signature.getInstance("SHA1withRSA");
            signature.initSign(privateKey);
            signature.update(content);
            return signature.sign();
        } catch (Exception var3) {
           log.error(var3.toString());
            return null;
        }
    }

    public static boolean verify(byte[] content, byte[] sign, PublicKey publicKey) {
        try {
            Signature signature = Signature.getInstance("SHA1withRSA");
            signature.initVerify(publicKey);
            signature.update(content);
            return signature.verify(sign);
        } catch (Exception var4) {
           log.error(var4.toString());
            return false;
        }
    }

    public static char[] bytes2hex(byte[] data) {
        int l = data.length;
        char[] out = new char[l << 1];
        int i = 0;

        for(int var4 = 0; i < l; ++i) {
            out[var4++] = DIGITS_LOWER[(240 & data[i]) >>> 4];
            out[var4++] = DIGITS_LOWER[15 & data[i]];
        }

        return out;
    }

    public static byte[] hex2bytes(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }

            return result;
        }
    }

    public static String encodeBase64(byte[] data) {
        return Base64.getEncoder().encodeToString(data);
    }

    public static byte[] decodeBase64(String text) {
        return Base64.getDecoder().decode(text);
    }
}
